﻿package editorPackage {
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.text.*;
	import flash.geom.Point;
	import fl.motion.Color;
	import flash.display.Shape;
	import flash.display.SimpleButton;
	import flash.utils.ByteArray;
    import flash.net.*;

	public class Editor extends MovieClip {
		
		var editWindow:EditWindow;
		var activeTool:ActiveButton;
		
		var textBoxProperties:TextBoxProperty;
		var pageProperties:PageProperty;
		var imageProperties:ImageProperty;
		
		var loader:FileLoader;
		var fr:FileReference; //Used for loading
		var cursor:String; //A string to determine what tool is being used.
		var elements:Array;
		var elementTypes:Array; 
		var firstClick:Boolean; // A switch to check if this is the first of two clicks
		var firstPos:Point; //The first click made when drawing a box
		var helperBox:Shape; //A guide shape to show the size of a box
		var keys:Array; //Boolean array to check if a key is pressed
		
		public function Editor() {
			cursor = "default";
			firstClick = true;
			elements = new Array();
			elementTypes = new Array();
			addToolButtons();
			editWindow = new EditWindow(); editWindow.x = 100; editWindow.y = 100; addChild(editWindow); editWindow.addEventListener(MouseEvent.CLICK, stageListener);
			helperBox = new Shape(); addChild(helperBox); editWindow.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveListener);
			keys = new Array(256);
			addEventListener(MouseEvent.MOUSE_UP, mouseUpListener);
			setupPropertyWindows();	
		}
		
		private function textBoxAction(e:Event):void { cursor = "textbox"; activeTool.y = 100; showProperties(cursor); textBoxProperties.dislocate(); }
		private function pointerAction(e:Event):void { cursor = "default"; activeTool.y = 40; showProperties(cursor); textBoxProperties.dislocate(); }
		private function eraserAction(e:Event):void { cursor = "eraser"; activeTool.y = 70; textBoxProperties.dislocate(); }
		private function imageAction(e:Event):void { cursor = "image"; activeTool.y = 130; showProperties(cursor); textBoxProperties.dislocate(); loadImage();}
		
		private function stageListener(e:MouseEvent):void {
			textBoxProperties.dislocate();
			switch (cursor) {
				case "textbox":
					if (firstClick) {
						firstPos = new Point(e.stageX, e.stageY);
						firstClick = false;
					} else {
						
						var t:TextBox = new TextBox(this, firstPos.x, firstPos.y, Math.abs(e.stageX - firstPos.x), Math.abs(e.stageY - firstPos.y), textBoxProperties.getKeyValue("font"), textBoxProperties.getKeyValue("size"));
						t.getTextField().setTextFormat(new TextFormat(t.getFont()));
						addChild(t.getTextField());
						elements.push(t);
						elementTypes.push(cursor);
						firstClick = true;
						textBoxProperties.configure(t);
					}
					break;
				case "default":
					showProperties(cursor);
					break;
			}
			
		}
		
		private function saveListener(e:Event):void {
			var xml:String = "<?xml version=\"1.0\"?>\n<Page Title=\""+pageProperties.getTitle()+"\" color=\"" + pageProperties.getColor() + "\">\n";
			for (var i:int = 0; i < elements.length; i++) {
				switch (String(elementTypes[i])) {
					//trace("Adding Some XML");
					case "textbox":	
						var t:TextBox = TextBox(elements[i]);
						xml += t.toString(i);
						break;
					case "image":
						var img:ImageObject = ImageObject(elements[i]);
						xml += img.toXML(i);
						break;
				}
			}
			xml += "</Page>";
			trace(xml);
			var ba:ByteArray = new ByteArray();
			ba.writeUTFBytes(xml);
			var fr:FileReference = new FileReference();
			fr.save(ba, "save.xml");
			
			trace("saving " + elementTypes.length + " elements");
		}
		
		private function loadListener(e:Event):void {
			trace(elements.length);
			loader = new FileLoader(this);
			loader.loadFile();
		}		
		
		//Show a box that helps to draw shapes
		private function showHelperBox(e:MouseEvent):void {
			if (!firstClick) {
				helperBox.graphics.clear();
				var X:int = firstPos.x; var Y:int = firstPos.y;
				var W:int = Math.abs(e.stageX - firstPos.x);
				var H:int = Math.abs(e.stageY - firstPos.y);
				helperBox.graphics.lineStyle(1,0x000000);
				helperBox.graphics.drawRect(X,Y,W,H);
			} else {
				helperBox.graphics.clear();
			}
		}
		
		private function mouseUpListener(e:MouseEvent):void {
			helperBox.graphics.clear();
			//For dragging objects
			for (var i:int = 0; i < elements.length; i++) {
				switch(elementTypes[i]) {
					case "textbox":
						var box:TextBox = TextBox(elements[i]);
						box.mUp();
						break;
					case "image":
						var img:ImageObject = ImageObject(elements[i]);
						img.mUp();
						break;
				}
			}
		}
		
		private function mouseMoveListener(e:MouseEvent):void {
			//Show the helper box (for drawing shapes)
			showHelperBox(e);
		}
		
		
		private function addToolButtons():void {			
			
			var pointerBtn:PointerBtn = new PointerBtn(); pointerBtn.x = 40; pointerBtn.y = 40; addChild(pointerBtn); pointerBtn.addEventListener(MouseEvent.CLICK, pointerAction);
			var eraseBtn:EraseBtn = new EraseBtn(); eraseBtn.x = 40; eraseBtn.y = 70; addChild(eraseBtn); eraseBtn.addEventListener(MouseEvent.CLICK, eraserAction);
			var textDrawBtn:toolBtn = new toolBtn(); textDrawBtn.x = 40; textDrawBtn.y = 100; addChild(textDrawBtn); textDrawBtn.addEventListener(MouseEvent.CLICK, textBoxAction);
			var imageBtn:ImageBtn = new ImageBtn(); imageBtn.x = 40; imageBtn.y = 130; addChild(imageBtn); imageBtn.addEventListener(MouseEvent.CLICK, imageAction);
			
			activeTool = new ActiveButton(); activeTool.x = 40; activeTool.y = 40; addChild(activeTool);
			
			createBasicButton(10, 738, "Save", saveListener);
			createBasicButton(10, 708, "Load", loadListener);
		}
		
		private function createBasicButton(X:int, Y:int, labelText:String, listener:Function) {
			var basicBtn:BasicButton = new BasicButton();
			basicBtn.x = X;
			basicBtn.y = Y;
			addChild(basicBtn);
			basicBtn.addEventListener(MouseEvent.CLICK, listener);
			var label:TextField = new TextField();
			label.text = labelText;
			label.mouseEnabled = false;
			label.x = X;
			label.y = Y;
			var format:TextFormat = new TextFormat();
			format.align = "center";
			label.width = 80;
			label.setTextFormat(format);
			addChild(label);
		}
		
		private function setupPropertyWindows() {
			textBoxProperties = new TextBoxProperty();
			pageProperties = new PageProperty(editWindow);
			imageProperties = new ImageProperty();
			addChild(pageProperties);
			
		}
		
		public function showProperties(tool:String, textbox:TextBox = null, image:ImageObject = null):void {
			//Show properties for tools
			switch(tool) {
				case "textbox":
					if (textbox != null) textBoxProperties.configure(textbox);
					addChild(textBoxProperties);
					if (contains(pageProperties)) removeChild(pageProperties);
					if (contains(imageProperties)) removeChild(imageProperties);
					break;
				case "default":
					if (contains(textBoxProperties)) removeChild(textBoxProperties);
					if (contains(imageProperties)) removeChild(imageProperties);
					addChild(pageProperties);
					break;
				case "image":
					if (contains(pageProperties)) removeChild(pageProperties);
					if (contains(textBoxProperties)) removeChild(textBoxProperties);
					if (image != null) imageProperties.configure(image);
					addChild(imageProperties);
			}
		}
		
		private function loadImage() {
			var img:ImageObject = new ImageObject(this);
			img.loadImage();
			addChild(img.getImage());
		}
		
	}
}
